package org.fedex.rg.activities;

import java.util.ArrayList;
import java.util.List;

import org.fedex.rg.R;
import org.fedex.rg.async.UpdatePlaylistTask;
import org.fedex.rg.async.callbacks.PlaylistUpdateFinishedListener;
import org.fedex.rg.model.Song;
import org.fedex.rg.servises.DownloadServise;
import org.fedex.rg.servises.PlaylistUpdateService;
import org.fedex.rg.servises.impl.MockDownloadServise;
import org.fedex.rg.servises.impl.RadioRocksPlaylistUpdater;
import org.fedex.rg.ui.DiffAdapter;
import org.fedex.rg.ui.SongAdapter;
import org.taptwo.android.widget.TitleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.danikula.aibolit.Aibolit;
import com.danikula.aibolit.annotation.InjectView;

public class MainActivity extends Activity implements
		PlaylistUpdateFinishedListener {

	private DownloadServise mDownloadServise = new MockDownloadServise(this);
	private PlaylistUpdateService mPlaylistUpdateServise = new RadioRocksPlaylistUpdater();

	@InjectView(R.id.viewflow)
	private ViewFlow mViewFlow;
	private List<Song> mSongs;
	private SongAdapter mAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Aibolit.doInjections(this);

		DiffAdapter adapter = new DiffAdapter(this);
		mViewFlow.setAdapter(adapter);
		TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
		indicator.setTitleProvider(adapter);
		mViewFlow.setFlowIndicator(indicator);

		/** To populate ListView in diff_view1.xml */
		ListView listView = (ListView) findViewById(R.id.listView1);
		mSongs = new ArrayList<Song>();
		this.mAdapter = new SongAdapter(this, R.layout.song_row, mSongs,
				mDownloadServise);
		listView.setAdapter(this.mAdapter);

		new UpdatePlaylistTask(this, mPlaylistUpdateServise, this).execute();

	}

	@Override
	public void onUpdateFinish(List<Song> songs) {
		if (songs != null && songs.size() > 0) {
			mAdapter.notifyDataSetChanged();
			for (int i = 0; i < songs.size(); i++) {
				mSongs.add(songs.get(i));
			}
		}
		mAdapter.notifyDataSetChanged();
	}

}